package com.um.jdy.app.user.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import com.alipay.api.AlipayApiException;
import com.um.jdy.app.user.entity.dto.WalletListQuery;
import com.um.jdy.app.user.entity.dto.WalletRechargeParam;
import com.um.jdy.app.user.entity.vo.*;
import com.um.jdy.app.user.manager.UserManager;
import com.um.jdy.app.user.manager.WalletManager;
import com.um.jdy.common.member.entity.po.MemberUser;
import com.um.jdy.common.member.entity.vo.MemberUserVO;
import com.um.jdy.common.member.entity.vo.RechargePackageVO;
import com.um.springboot.starter.annotation.ActionLogRequired;
import com.um.springboot.starter.entity.Page;
import com.um.springboot.starter.orm.entity.Response;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;


@Api(tags = "钱包")
@RestController
@RequestMapping("/api/v1/user/wallet")
public class WalletController {

    @Autowired
    WalletManager walletManager;

    @Autowired
    UserManager userManager;

    @ApiOperation("查询钱包详情")
    @GetMapping
    public Response<WalletDetailVO> get() {
        return Response.success(walletManager.get());
    }

    @ApiOperation("查询钱包套餐列表")
    @GetMapping("package/list")
    @SaCheckLogin
    public Response<List<RechargePackageVO>> getPackageList() {
        return Response.success(walletManager.getPackageList());
    }

    @ApiOperation("钱包充值")
    @PostMapping("create_order")
    @SaCheckLogin
    public Response recharge(@RequestBody @Valid WalletRechargeParam param) {
        return Response.success(walletManager.recharge(param));
    }

    @ApiOperation("查询钱包余额")
    @GetMapping("balance")
    public Response<BalanceVO> getBalance() {
        BalanceVO balanceVO = new BalanceVO();
        balanceVO.setBalance(walletManager.getBalance());
        balanceVO.setCanCashout(walletManager.getCanCashoutBalance());
        return Response.success(balanceVO);
    }

    @ApiOperation("查询钱包明细")
    @GetMapping("list")
    @SaCheckLogin
    public Response<Page<WalletListVO>> getList(@ModelAttribute WalletListQuery query){
        return Response.success(walletManager.getWalletPage(query));
    }

    @ApiOperation("查询账单明细")
    @GetMapping("bill/list")
    @SaCheckLogin
    public Response<Page<UserBillListVO>> getBillList(@ModelAttribute WalletListQuery query){
        return Response.success(walletManager.getBillPage(query));
    }

    @ApiOperation("押金提现")
    @PostMapping("deposit_cashout")
    @SaCheckLogin
    public Response<DepositCashoutVO> depositCashout() {
        return Response.success(walletManager.depositCashout());
    }

    @ApiOperation("余额提现")
    @PostMapping("cashout")
    @SaCheckLogin
    public Response cashout() {
        MemberUserVO user = userManager.get();
        return walletManager.cashout(user.getMemberUserId(),user.getComId());
    }

    /**
     * 微信支付充值回调
     * @return
     */
    @ActionLogRequired
    @RequestMapping("wxpay/notify/{com_id}")
    public Response wxPayNotify(HttpServletRequest request, @PathVariable String com_id) {
        walletManager.wxPayNotify(request, com_id);
        return Response.success();
    }

    @ActionLogRequired
    @RequestMapping(value = "alipay/notify")
    @ResponseBody
    public String alipayNotify(HttpServletRequest request) throws AlipayApiException {
        walletManager.aliPayNotify(request);
        // 回复支付宝
        return "success";
    }

}
